/* lookup.i - Standard Include file for lookup.i                              */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.43 $                                                          */
/*V8:ConvertMode=NoConvert                                                    */
/******************************** History *************************************/
/*  Revision: 8.5    Created: 04/24/97  *J1PW*  By: Allan Doane               */
/*  Revision: 8.5   Modified: 06/05/97  *J1SN*  By: Allan Doane               */
/*  Revision: 8.5   Modified: 06/16/97  *J1TF*  By: Bill Pedersen             */
/*  Revision: 8.5   Modified: 06/29/97  *J1VL*  By: Bill Pedersen             */
/*  Revision: 8.5   Modified: 07/18/97  *J1X2*  By: Bill Pedersen             */
/*  Revision: 8.5   Modified: 07/22/97  *J1XC*  By: Jean Miller               */
/*  Revision: 8.5   Modified: 07/22/97  *J1XD*  By: Bill Pedersen             */
/*  Revision: 8.5   Modified: 07/29/97  *J1XH*  By: Bill Pedersen             */
/*  Revision: 8.5   Modified: 10/28/97  *J24F*  By: David Sinclaire           */
/*  Revision: 8.5   Modified: 01/12/98  *J278*  By: Mark B. Smith             */
/*  Revision: 8.5   Modified: 02/12/98  *J2F6*  By: Mark B. Smith             */
/*  Revision: 8.5   Modified: 04/17/98  *J2KL*  BY: Raphael T.                */
/*  Revision: 8.5   Modified: 09/21/98  *J30K*  BY: Raphael T.                */
/*  Revision: 8.5   Modified: 10/07/98  *J31G*  BY: Vijaya Pakala.            */
/*  Revision: 8.5   Modified: 10/23/98  *J32R*  BY: A. Philips                */
/*  Revision: 8.5   Modified: 11/05/98  *J2XR*  BY: Viswanathan M             */
/*  Revision: 9.0   Modified: 11/23/98  *M01G*  BY: Raphael T.                */
/*  Revision: 9.0   Modified: 02/22/99  *M08Z*  BY: A. Philips                */
/*  Revision: 9.0   Modified: 03/04/99  *J365*  BY: A. Philips                */
/*  Revision: 9.0   Modified: 03/13/99  *M0BD*  BY: Alfred Tan                */
/*  Revision: 9.0   Modified: 03/31/99  *J3D1*  BY: A. Philips                */
/*  Revision: 9.0   Modified: 07/06/99  *J3HM*  BY: J. Fernando               */
/*  Revision: 9.0   Modified: 08/27/99  *K22D*  BY: A. Philips                */
/*  Revision: 9.0   Modified: 12/15/99  *M0GW*  BY: J. Fernando               */
/*  Revision: 9.1   Modified: 03/24/00  *N098*  By: D.Taylor                  */
/*  Revision: 9.1   Modified: 08/13/00  *N0KR*  By: Mark Brown                */
/*  Revision: 9.1   Modified: 09/05/00  *M0S6*  BY: Falguni Dalal             */
/*  Revision: 1.40       BY: Falguni Dalal        DATE: 08/10/01  ECO: *N11F* */
/*  Revision: 1.41       BY: John Corda           DATE: 04/03/02  ECO: *M1X1* */
/*  Revision: 1.42       BY: Jean Miller          DATE: 06/06/02  ECO: *N1KP* */
/* $Revision: 1.43 $     BY: Jean Miller          DATE: 06/12/02  ECO: *P088* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

CREATE WIDGET-POOL.

/* ***************************  Definitions  ************************** */

/* Parameters Definitions ---                                           */
define input  parameter h-manager as handle no-undo.

/* Update this preprocessor with the last ECO that touched this include */
&GLOBAL-DEFINE lookupi lookup.i N1KP

/* Shared Variable Definitions ---                                      */
define shared variable swindow_recid as recid no-undo.

/* Local Variable Definitions ---                                       */
define variable c-browse-name      as character initial "{&BR-NAME}" no-undo.
define variable c-frame-field-name as character no-undo.
/* Sequencer for temp table */
define variable i-tt-seq           as integer no-undo.
/* incrementer for temp table */
define variable i-tt-seq-inc       as integer no-undo.
/* tracks column movement */
define variable i-prev-col         as integer initial 1 no-undo.
/* current column */
define variable i-curr-index       as integer no-undo.
define variable h-index-cb         as handle no-undo.
/* handle to the query pp  */
define variable h-query            as handle no-undo.
define variable h-frame            as widget-handle no-undo.
define variable h-search-field     as widget-handle no-undo.
define variable r-key-recno        as recid no-undo.
/* {&ROW-ID} of temp-table  */
define variable r-temp-id          as rowid no-undo.

define variable h-temp             as handle no-undo.
define variable h-cell             as handle no-undo.
define variable i-temp-index       as integer no-undo.
define variable l-search-ok        as logical no-undo.

define variable c-index-cb as character
   format "x(18)"
   view-as combo-box inner-lines {&Q-NUM-FILT-VALS}
   &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
      SIZE 22 BY 1
   &ELSE
      SIZE 22 BY 1
   &ENDIF no-undo.

/* Variables to control the search message and positioning of records */
/* when a new "chunk" is displayed.                                   */

/*rowid of the temp table where display should begin from.*/
define variable r-tempb-id    as rowid     no-undo.
/*holds the search message text*/
define variable c-srch-msg    as character no-undo.
/*index chars for type-ahead indexing*/
define variable c-index-val   as character no-undo.

define variable c-scroll-msg  as character format "x(35)"
   view-as text initial "" no-undo.
/* Trap last event for later testing */
define variable c-prev-event  as character no-undo.
define variable l-typeahead-timed-out as logical no-undo.
define variable l-done-query  as logical no-undo.
define variable i-temp-seq    as integer no-undo initial 1.

/* Local variable definitions from browse definition */
{&BR-LOCAL-VAR-1}
{&BR-LOCAL-VAR-2}
{&BR-LOCAL-VAR-3}
{&BR-LOCAL-VAR-4}
{&BR-LOCAL-VAR-5}
{&BR-LOCAL-VAR-6}
{&BR-LOCAL-VAR-7}

/* Arbitrary small number for wait-for pause */
&SCOPED-DEFINE TYPEAHEAD-PAUSE 1.25


&IF '{&WINDOW-SYSTEM}' = 'TTY' &THEN
   &SCOPED-DEFINE CHUNK-SIZE 24
   /* Arbitrary size to allow for some scrolling and best for CHUI */
&ELSE
    &SCOPED-DEFINE CHUNK-SIZE 24
    /* Arbitrary size to allow for some scrolling and best for GUI */
&ENDIF

&IF {&BR-BROWSE-WIDTH} < 73 &THEN
   &SCOPED-DEFINE BROWSE-WIDTH
&ELSE
   &SCOPED-DEFINE BROWSE-WIDTH WIDTH 75
&ENDIF

/* Define a temp table that looks like the join */
define temp-table tt-browse
   field tt-recid              as   recid
   field tt-seq                as   integer
   field {&TT-Q-BROWSE-DEF-EXPR-1} like {&Q-BROWSE-DEF-EXPR-1}
   &IF {&Q-NUM-FILT-VALS} > 1 &THEN
      field {&TT-Q-BROWSE-DEF-EXPR-2} like {&Q-BROWSE-DEF-EXPR-2}
   &ENDIF
   &IF {&Q-NUM-FILT-VALS} > 2 &THEN
      field {&TT-Q-BROWSE-DEF-EXPR-3} like {&Q-BROWSE-DEF-EXPR-3}
   &ENDIF
   &IF {&Q-NUM-FILT-VALS} > 3 &THEN
      field {&TT-Q-BROWSE-DEF-EXPR-4} like {&Q-BROWSE-DEF-EXPR-4}
   &ENDIF
   &IF {&Q-NUM-FILT-VALS} > 4 &THEN
      field {&TT-Q-BROWSE-DEF-EXPR-5} like {&Q-BROWSE-DEF-EXPR-5}
   &ENDIF
   &IF {&Q-NUM-FILT-VALS} > 5 &THEN
      field {&TT-Q-BROWSE-DEF-EXPR-6} like {&Q-BROWSE-DEF-EXPR-6}
   &ENDIF
   &IF {&Q-NUM-FILT-VALS} > 6 &THEN
      field {&TT-Q-BROWSE-DEF-EXPR-7} like {&Q-BROWSE-DEF-EXPR-7}
   &ENDIF
   index tt-seq is unique is primary
         tt-seq.

/* Name of first Frame and/or Browse and/or first Query                 */
&SCOPED-DEFINE frame-NAME Dialog-Frame
&SCOPED-DEFINE BROWSE-NAME BROWSE-2

/* Internal Tables (found by Frame, Query & Browse Queries)             */
&SCOPED-DEFINE INTERNAL-TABLES tt-browse

/* Definitions for BROWSE BROWSE-2                                      */
&SCOPED-DEFINE FIELDS-IN-QUERY-BROWSE-2 {&TT-Q-BROWSE-DEF-EXPR-1} ~
{&BR-K-COL-LABEL-1} {&BR-COLUMN-LABEL-1} {&BR-FIELD-FRMT-1} ~
{&TT-Q-BROWSE-DEF-EXPR-2} {&BR-K-COL-LABEL-2} {&BR-COLUMN-LABEL-2} ~
{&BR-FIELD-FRMT-2} {&TT-Q-BROWSE-DEF-EXPR-3} {&BR-K-COL-LABEL-3} ~
{&BR-COLUMN-LABEL-3} {&BR-FIELD-FRMT-3} {&TT-Q-BROWSE-DEF-EXPR-4} ~
{&BR-K-COL-LABEL-4} {&BR-COLUMN-LABEL-4} {&BR-FIELD-FRMT-4} ~
{&TT-Q-BROWSE-DEF-EXPR-5} {&BR-K-COL-LABEL-5} {&BR-COLUMN-LABEL-5} ~
{&BR-FIELD-FRMT-5} {&TT-Q-BROWSE-DEF-EXPR-6} {&BR-K-COL-LABEL-6} ~
{&BR-COLUMN-LABEL-6} {&BR-FIELD-FRMT-6} {&TT-Q-BROWSE-DEF-EXPR-7} ~
{&BR-K-COL-LABEL-7} {&BR-COLUMN-LABEL-7} {&BR-FIELD-FRMT-7}
&SCOPED-DEFINE ENABLED-FIELDS-IN-QUERY-BROWSE-2
&SCOPED-DEFINE FIELD-PAIRS-IN-QUERY-BROWSE-2
&SCOPED-DEFINE SELF-NAME BROWSE-2
&SCOPED-DEFINE OPEN-QUERY-BROWSE-2 OPEN QUERY BROWSE-2 FOR EACH tt-browse no-lock.
&SCOPED-DEFINE TABLES-IN-QUERY-BROWSE-2 tt-browse
&SCOPED-DEFINE FIRST-TABLE-IN-QUERY-BROWSE-2 tt-browse

/* Definitions for frame DIALOG-frame                                         */
&SCOPED-DEFINE OPEN-BROWSERS-IN-QUERY-DIALOG-frame  ~{&OPEN-QUERY-BROWSE-2}

/* Query definitions                                                    */
DEFINE QUERY BROWSE-2 FOR tt-browse SCROLLING.

/* Browse definitions                                                   */
DEFINE BROWSE BROWSE-2
   QUERY BROWSE-2 no-lock
   display
    {&TT-Q-BROWSE-DEF-EXPR-1}  {&BR-K-COL-LABEL-1}  {&BR-COLUMN-LABEL-1}  {&BR-FIELD-FRMT-1}
    {&TT-Q-BROWSE-DEF-EXPR-2}  {&BR-K-COL-LABEL-2}  {&BR-COLUMN-LABEL-2}  {&BR-FIELD-FRMT-2}
    {&TT-Q-BROWSE-DEF-EXPR-3}  {&BR-K-COL-LABEL-3}  {&BR-COLUMN-LABEL-3}  {&BR-FIELD-FRMT-3}
    {&TT-Q-BROWSE-DEF-EXPR-4}  {&BR-K-COL-LABEL-4}  {&BR-COLUMN-LABEL-4}  {&BR-FIELD-FRMT-4}
    {&TT-Q-BROWSE-DEF-EXPR-5}  {&BR-K-COL-LABEL-5}  {&BR-COLUMN-LABEL-5}  {&BR-FIELD-FRMT-5}
    {&TT-Q-BROWSE-DEF-EXPR-6}  {&BR-K-COL-LABEL-6}  {&BR-COLUMN-LABEL-6}  {&BR-FIELD-FRMT-6}
    {&TT-Q-BROWSE-DEF-EXPR-7}  {&BR-K-COL-LABEL-7}  {&BR-COLUMN-LABEL-7}  {&BR-FIELD-FRMT-7}

   /*PROVERSION 8 SPECIFIC LOGIC*********************************** */
    &IF PROVERSION >= "8.0"
    &THEN
       ENABLE
          &IF lookup("1","{&BR-INDEXED-FIELDS}") > 0
          &THEN
             {&TT-Q-BROWSE-DEF-EXPR-1}
          &ENDIF
          &IF lookup("2","{&BR-INDEXED-FIELDS}") > 0
          &THEN
             {&TT-Q-BROWSE-DEF-EXPR-2}
          &ENDIF
          &IF lookup("3","{&BR-INDEXED-FIELDS}") > 0
          &THEN
             {&TT-Q-BROWSE-DEF-EXPR-3}
          &ENDIF
          &IF lookup("4","{&BR-INDEXED-FIELDS}") > 0
          &THEN
             {&TT-Q-BROWSE-DEF-EXPR-4}
          &ENDIF
          &IF lookup("5","{&BR-INDEXED-FIELDS}") > 0
          &THEN
             {&TT-Q-BROWSE-DEF-EXPR-5}
          &ENDIF
          &IF lookup("6","{&BR-INDEXED-FIELDS}") > 0
          &THEN
             {&TT-Q-BROWSE-DEF-EXPR-6}
          &ENDIF
          &IF lookup("7","{&BR-INDEXED-FIELDS}") > 0
          &THEN
             {&TT-Q-BROWSE-DEF-EXPR-7}
          &ENDIF
       WITH 18 DOWN     /* 8  -->   18 */
       {&BROWSE-WIDTH}
       SEPARATORS
       NO-assign
       NO-ROW-MARKERS
    &ELSE             /*PROVERSION general*/
       WITH 18 DOWN
       {&BROWSE-WIDTH}
       SEPARATORS
    &ENDIF           /*PROVERSION 8 specific logic*/
    FONT 21.


/* ************************  Frame Definitions  *********************** */

DEFINE frame Dialog-Frame
   BROWSE-2 AT
      &IF '{&WINDOW-SYSTEM}' = 'TTY' &THEN
         row 2 column 2
      &ELSE
         row 3 column 2
      &ENDIF
   c-index-cb   at row 1  column 1 no-label
   c-scroll-msg at row 12 column 2 no-label
   space(1.66)
   /*skip(2.84)*/
with view-as dialog-box keep-tab-order side-labels no-underline
three-d scrollable title c-brw-desc.

/* SET EXTERNALIZED LABELS. */
setBrowseLabels(input {&BROWSE-NAME}:handle).

/* *************************  Create Window  ************************** */

assign
   frame Dialog-Frame:SCROLLABLE = false
   frame Dialog-Frame:HIDDEN     = true.

if BROWSE-2:NUM-COLUMNS > 2 then
   BROWSE-2:NUM-LOCKED-COLUMNS in frame Dialog-frame = 2.


/* ************************  Control Triggers  ************************ */

&SCOPED-DEFINE BROWSE-NAME BROWSE-2
&SCOPED-DEFINE SELF-NAME BROWSE-2

on WINDOW-CLOSE of frame Dialog-Frame do:
   swindow_recid = ?.
   apply "END-ERROR" to self.
end.

on END-ERROR of frame Dialog-Frame do:
   /* Tell manager that we're going away */
   run lu-shutdown in h-manager
      (input c-browse-name).
   h-query = ?.
end.

/*The following triggers are only available for Progress version 8*/
&IF PROVERSION >= "8.0" &THEN
   on scroll-notify of BROWSE-2 in frame {&FRAME-NAME} do:
      /*Toggle to indicate that the scroll bar has been pressed   */
      /*This will activate the possible need of the search message*/
      /*if "off-end" or "off-home" is triggered.                  */
      assign
         c-index-val  = ""
         c-prev-event = last-event:label
         i-temp-seq   = 0.
      if valid-handle(h-cell) then
         run destroy-widget
            (input h-cell).
      h-cell = ?.
   end.

   on row-display of browse {&BROWSE-NAME} do:
      /*Needed to remove the cell when scrolling occurs*/
      apply "ENTRY" to h-search-field.
      if valid-handle(h-cell) then
         run destroy-widget
            (input h-cell).
      h-cell = ?.
   end.

   on START-SEARCH of {&BROWSE-NAME} in frame {&FRAME-NAME}
   do:
      define variable h-temp as handle no-undo.
      define variable i-temp-index as integer no-undo.

      assign
         l-search-ok = true

         /*Initialize typeahead index & establish column context*/
         h-temp = {&BROWSE-NAME}:current-column
         i-temp-index = lookup(h-temp:label,h-index-cb:list-items).

      if i-temp-index = 0
      then do:
         /*Do not allow if the column is not a selected index*/
         apply "end-search" to browse {&BROWSE-NAME}.
         l-search-ok = false.
      end.

      else do:

         /*Set the index to the current column*/
         if i-curr-index <> i-temp-index
         then do:
            assign
               c-index-val = ""
               i-curr-index = i-temp-index.

            /* ADD CHECKS TO ENSURE THAT THE VALUE OF THE ENTRY NUMBER SUPPLIED  */
            /* TO THE ENTRY FUNCTION IS NEITHER <= 0 NOR GREATER THAN THE NUMBER */
            /* OF ENTRIES IN THE COMMA-SEPERATED LIST.                           */
            if not(i-curr-index <= 0
               or i-curr-index > h-index-cb:num-items)
            then
               h-index-cb:screen-value = entry(i-curr-index,h-index-cb:list-items).

            /*Apply entry to establish context on browse so you don't*/
            /*trigger the end-search prematurely.                    */
            apply "ENTRY" to browse {&BROWSE-NAME}.

            /*Create the search field since it has changed*/
            run create-search-field.
         end.

      end.

      /*Set the screen-value to the typeahead index*/
      h-search-field:screen-value = c-index-val.

      return no-apply.

   end.

   &IF lookup("1","{&BR-INDEXED-FIELDS}") > 0 &THEN
      on ENTRY of {&TT-Q-BROWSE-DEF-EXPR-1} in browse {&BROWSE-NAME} do:
         run cell-entry.
         return no-apply.
      end.

      on LEAVE of {&TT-Q-BROWSE-DEF-EXPR-1} in browse {&BROWSE-NAME} do:
         if valid-handle(h-cell) then
            run destroy-widget
              (input h-cell).
         h-cell = ?.
      end.
   &ENDIF

   &IF lookup("2","{&BR-INDEXED-FIELDS}") > 0 &THEN
      on ENTRY of {&TT-Q-BROWSE-DEF-EXPR-2} in browse {&BROWSE-NAME} do:
         run cell-entry.
         return no-apply.
      end.

      on LEAVE of {&TT-Q-BROWSE-DEF-EXPR-2} in browse {&BROWSE-NAME} do:
         if valid-handle(h-cell) then
            run destroy-widget
              (input h-cell).
         h-cell = ?.
      end.
   &ENDIF

   &IF lookup("3","{&BR-INDEXED-FIELDS}") > 0 &THEN
      on ENTRY of {&TT-Q-BROWSE-DEF-EXPR-3} in browse {&BROWSE-NAME} do:
         run cell-entry.
         return no-apply.
      end.

      on LEAVE of {&TT-Q-BROWSE-DEF-EXPR-3} in browse {&BROWSE-NAME} do:
         if valid-handle(h-cell) then
            run destroy-widget
               (input h-cell).
         h-cell = ?.
      end.
   &ENDIF

   &IF lookup("4","{&BR-INDEXED-FIELDS}") > 0 &THEN
      on ENTRY of {&TT-Q-BROWSE-DEF-EXPR-4} in browse {&BROWSE-NAME} do:
         run cell-entry.
         return no-apply.
      end.

      on LEAVE of {&TT-Q-BROWSE-DEF-EXPR-4} in browse {&BROWSE-NAME} do:
         if valid-handle(h-cell) then
            run destroy-widget
               (input h-cell).
         h-cell = ?.
      end.
   &ENDIF

   &IF lookup("5","{&BR-INDEXED-FIELDS}") > 0 &THEN
      on ENTRY of {&TT-Q-BROWSE-DEF-EXPR-5} in browse {&BROWSE-NAME} do:
         run cell-entry.
         return no-apply.
      end.

      on LEAVE of {&TT-Q-BROWSE-DEF-EXPR-5} in browse {&BROWSE-NAME} do:
         if valid-handle(h-cell) then
            run destroy-widget
               (input h-cell).
         h-cell = ?.
      end.
   &ENDIF

   &IF lookup("6","{&BR-INDEXED-FIELDS}") > 0 &THEN
      on ENTRY of {&TT-Q-BROWSE-DEF-EXPR-6} in browse {&BROWSE-NAME} do:
         run cell-entry.
         return no-apply.
      end.

      on LEAVE of {&TT-Q-BROWSE-DEF-EXPR-6} in browse {&BROWSE-NAME} do:
         if valid-handle(h-cell) then
            run destroy-widget
               (input h-cell).
         h-cell = ?.
      end.
   &ENDIF

   &IF lookup("7","{&BR-INDEXED-FIELDS}") > 0 &THEN
      on ENTRY of {&TT-Q-BROWSE-DEF-EXPR-7} in browse {&BROWSE-NAME} do:
         run cell-entry.
         return no-apply.
      end.

      on LEAVE of {&TT-Q-BROWSE-DEF-EXPR-7} in browse {&BROWSE-NAME} do:
         if valid-handle(h-cell) then
            run destroy-widget
               (input h-cell).
         h-cell = ?.
      end.
   &ENDIF

&ENDIF    /*END OF PROVERSION 8 ONLY LOGIC********************/

on ANY-KEY OF {&BROWSE-NAME} in frame {&FRAME-NAME} do:

   assign
      c-scroll-msg:screen-value = ""
      c-prev-event              = last-event:label
      i-temp-seq                = 0.

   if last-event:function = "BACKSPACE" or
      last-event:function = "DELETE-CHARACTER"
   then do:

      if length(c-index-val) > 0
      then do:

         c-index-val =
            substring(c-index-val,1,(length(c-index-val,"raw") - 1),"raw").

         run get-new-query
            (input i-curr-index,
             input c-index-val).
      end.

      else bell.
      return no-apply.

   end.

end.

on ANY-PRINTABLE of {&BROWSE-NAME} do:
   /*Accumulte keystokes for typeahead indexing   */
   /*If a query has been run, then the typeahead  */
   /*must be cleared if a new key is entered. This*/
   /*allows the backspace to keep the typeahead   */
   /*for additional query adjustment.             */

   if not l-search-ok then do:
      bell.
      return no-apply.
   end.

   if l-typeahead-timed-out then
      assign
         c-index-val           = ""
         l-typeahead-timed-out = false.

   assign
      c-index-val               = c-index-val + last-event:label
      c-scroll-msg:screen-value = ""
      l-done-query              = false
      c-prev-event              = last-event:label
      i-temp-seq                = 0.

end.

on CURSOR-DOWN of {&BROWSE-NAME} in frame {&FRAME-NAME} do:
    define variable l-at-bot as logical no-undo.

    assign
       c-scroll-msg:screen-value = ""
       c-index-val               = ""
       c-prev-event              = last-event:label
       i-temp-seq                = 0.

    run at-bottom
       (output l-at-bot).

    if l-at-bot then
       return no-apply.
end.

on CURSOR-UP of {&BROWSE-NAME} in frame {&FRAME-NAME} do:
    define variable l-at-top as logical no-undo.

    assign
       c-scroll-msg:screen-value = ""
       c-index-val               = ""
       c-prev-event              = last-event:label
       i-temp-seq                = 0.

    run at-top
       (output l-at-top).
    if l-at-top then
       return no-apply.
end.


on PAGE-DOWN of {&BROWSE-NAME} in frame {&FRAME-NAME} do:
    define variable l-near-bottom as logical no-undo.

    assign
       c-scroll-msg:screen-value = ""
       c-index-val               = ""
       c-prev-event              = last-event:label.

    /*Capture the sequence number of the current */
    /*record. This will be used for repositioning*/
    /*this record to the top if off-end is       */
    /*encountered.                               */
    if available tt-browse then
       i-temp-seq = tt-browse.tt-seq.

    run near-bottom
       (output l-near-bottom).

    if l-near-bottom
    then do:
       run get-last-chunk.
       return no-apply.
    end.

end.

on PAGE-UP of {&BROWSE-NAME} in frame {&FRAME-NAME} do:
    define variable l-near-top as logical no-undo.

    assign
       c-scroll-msg:screen-value = ""
       c-index-val = ""
       c-prev-event = last-event:label
       i-temp-seq = 0.

    run near-top
        (output l-near-top).

    if l-near-top then do:
        run get-first-chunk.
        return no-apply.
    end.
end.

on return of browse {&BROWSE-NAME}
or mouse-select-dblclick of browse {&BROWSE-NAME} do:
   run q-ok.
   apply "END-ERROR" TO frame {&FRAME-NAME}.
end.

on F9 of browse {&BROWSE-NAME} do:
   apply "CURSOR-UP" to browse {&BROWSE-NAME}.
   return no-apply.
end.

on F10 of browse {&BROWSE-NAME} do:
   apply "CURSOR-DOWN" to browse {&BROWSE-NAME}.
   return no-apply.
end.

on SHIFT-F9 of browse {&BROWSE-NAME} do:
   apply "HOME" to browse {&BROWSE-NAME}.
   return no-apply.
end.

on SHIFT-F10 of browse {&BROWSE-NAME} do:
   apply "END" to browse {&BROWSE-NAME}.
   return no-apply.
end.

on CTRL-F9 of browse {&BROWSE-NAME} do:
   apply "PAGE-UP" to browse {&BROWSE-NAME}.
   return no-apply.
end.

on CTRL-F10 of browse {&BROWSE-NAME} do:
   apply "PAGE-DOWN" to browse {&BROWSE-NAME}.
   return no-apply.
end.

on value-changed of browse {&BROWSE-NAME} do:
   run find-recid-and-screen-value.
end.

on value-changed of c-index-cb do:
   c-index-val = "".

   if not(h-index-cb:lookup(h-index-cb:screen-value) <= 0
       or h-index-cb:lookup(h-index-cb:screen-value) >
          h-index-cb:num-items)
   then
      i-curr-index =
         integer(entry(h-index-cb:lookup(h-index-cb:screen-value),
                      h-index-cb:private-data)).

   run get-new-query
        (input i-curr-index,
         input "").

   if return-value <> "ERROR" then
      run position-col-focus.

   return no-apply.
end.

on HELP of frame {&FRAME-NAME} do:

   &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
      {gprun.i 'gphelp.p'
         "('FIELD_HELP', execname, input c-frame-field-name )" }
   &ELSE
      {gprun.i 'gphelp.p'
         "('FIELD_HELP', execname, input global-drill-handle:name )" }
   &ENDIF

   /*Need to make window visible again*/
   self:visible = true.
   apply "entry" to h-search-field.

end.

on END of BROWSE-2 in frame DIALOG-frame do:
   do with frame {&FRAME-NAME}:
      assign
         c-scroll-msg:screen-value = ""
         c-index-val = ""
         c-prev-event = last-event:label
         i-temp-seq = 0.
       run get-last-chunk.
    end.
end.

on HOME of BROWSE-2 in frame DIALOG-frame do:
   do with frame {&FRAME-NAME}:
      assign
         c-scroll-msg:screen-value = ""
         c-index-val = ""
         c-prev-event = last-event:label
         i-temp-seq = 0.
      run get-first-chunk.
   end.
end.

on OFF-END of BROWSE-2 in frame DIALOG-frame do:

   define variable h-temp as handle no-undo.
   define variable l-buf-available as logical no-undo.

   /* ALLOW FURTHER NAVIGATION ONLY WHEN MORE RECORDS */
   /* EXISTS FOR DISPLAYING.                          */
   run buf-available in h-query
      (output l-buf-available).

  if num-results("{&BROWSE-NAME}") <> 0 and
     l-buf-available
  then do with frame {&FRAME-NAME}:

    if c-srch-msg = "" then
       run get-srch-msg.

    c-scroll-msg:screen-value = c-srch-msg.

    run reposition-tt-query
       (input "LAST").
    run reposition-db-query
       (input "LAST").
    run get-next-chunk.

  end.

  if c-prev-event = "CURSOR-DOWN" and
     l-buf-available
  then
     apply c-prev-event to browse {&BROWSE-NAME}.


  /* CODE TO BRING BACK FOCUS TO BROWSE  */
  if num-results("{&BROWSE-NAME}") <> 0 then do:
    &IF PROVERSION >= "8.0" &THEN
       h-temp = {&BROWSE-NAME}:current-column.
       if valid-handle(h-temp) then
         apply "ENTRY" to h-temp.
    &ELSE
       /*Proversion 7 support*/
       apply "ENTRY" to browse {&BROWSE-NAME}.
    &ENDIF
  end.
  else
    apply "ENTRY" to h-search-field.

  c-prev-event = last-event:label.

end.

on OFF-HOME of BROWSE-2 in frame DIALOG-frame do:
   define variable l-near-top as logical no-undo.

   if c-srch-msg = "" then
      run get-srch-msg.

   run near-top
      (output l-near-top).
   if l-near-top then do:
      run get-first-chunk.
      return no-apply.
   end.

   do with frame {&FRAME-NAME}:

      c-scroll-msg:screen-value = c-srch-msg.

      run reposition-tt-query
         (input "FIRST").
      run reposition-db-query
         (input "FIRST").
      run get-prev-chunk.

   end.

    /*If the previous event before going off-end was a */
    /*PAGE-up OR CURSOR-up, then reapply those         */
    /*events.                                          */
    if c-prev-event = "PAGE-UP"
    or c-prev-event = "CURSOR-UP"
    then
       apply c-prev-event to browse {&BROWSE-NAME}.

    c-prev-event = last-event:label.

end.

/* ***************************  Main Block  *************************** */
/* Parent the dialog-box to the ACTIVE-WINDOW, if there is no parent.   */
if valid-handle(ACTIVE-WINDOW) and
   frame {&FRAME-NAME}:PARENT = ?
then
   frame {&FRAME-NAME}:PARENT = ACTIVE-WINDOW.

/* HIDE THE frame WHILE ADJUSTING frame'S PROPERTIES. */
frame {&FRAME-NAME}:hidden = true.

run q-adjust-widgets.

/* Now enable the interface and wait for the exit condition.            */
/* (NOTE: handle ERROR and END-KEY so cleanup code will always fire.    */
MAIN-BLOCK:
do on error   undo MAIN-BLOCK, leave MAIN-BLOCK
   on end-key undo MAIN-BLOCK, leave MAIN-BLOCK:

   assign
      c-frame-field-name = frame-field
      swindow_recid = ?.

   run enable_UI.

   if return-value = "ERROR" then return error "ERROR".

   /* Logic to place a time-out feature on the wait-for to allow               */
   /* for typeahead index querying for the browse. After the timeout period,   */
   /* the index will be reset if another printable character is entered.       */
   /* Backspace & delete-character are not subject to the timeout; thus you    */
   /* can always backward adjust the query reseult set.                        */
   do while true:

      /*First wait-for will exit on GO or if any printable character is entered*/
      WAIT-FOR GO of frame {&FRAME-NAME}
            or ANY-PRINTABLE of BROWSE {&BROWSE-NAME}.

      if last-event:function = "go" then leave.

      apply lastkey to self.

      /*Second wait-for acts as a timer to receive any printable keys, but will*/
      /*time out for processing of the index.                                  */
      WAIT-FOR GO of BROWSE {&BROWSE-NAME} pause {&TYPEAHEAD-PAUSE}.

      if c-index-val > "" and not l-done-query
      then do:

         run get-new-query
            (input i-curr-index,
             input c-index-val).

         if return-value = "ERROR" then
            return error.

         assign
            l-typeahead-timed-out = true
            l-done-query = true
            i-temp-index = i-curr-index
            l-search-ok = false.

         /*PROVERSION 8 specific logic - needed only for cell entry*/
         &IF PROVERSION >= "8.0" &THEN
            /*Apply entry back to the browse*/
            do with frame {&FRAME-NAME}:

               if available(tt-browse) and valid-handle(h-query)
               then do:

                  /*Need to initially get focus so fill-in will be created*/
                  apply "ENTRY" to browse {&BROWSE-NAME}.

                  /*Initialize the combo-box value*/
                  if not(i-temp-index <= 0
                        or i-temp-index > h-index-cb:num-items)
                  then
                     h-index-cb:screen-value = h-index-cb:entry(i-temp-index).

                 /*Position the cursor on cell*/
                 run position-col-focus.

                 /*Set the screen-value of the fill-in to the value typed*/
                 h-search-field:screen-value = c-index-val.

              end.

            end.
         &ENDIF

      end.  /* if c-index-val > "" and not done-query */

   end. /* do while true */

end. /* main-block */

run disable_UI.


/* **********************  Internal Procedures  *********************** */

PROCEDURE at-bottom :
/*------------------------------------------------------------------------------
  Purpose:     to test if we are at the bottom of the query.
  Parameters:  output p-at-bottom: logical
  Notes:       There are two ways to be at the bottom of a query:
                1) to be on the record with the Maximum sequence number
                    (having gotten there by hitting the "END" key).
                2) to be on the last record of the query
                    (having scrolled forward through it).
------------------------------------------------------------------------------*/
   define output parameter p-at-bottom as logical initial yes no-undo.

   if available(tt-browse) and valid-handle(h-query) then do:
      if tt-seq = 9999999 - 1 then
         p-at-bottom = true.
      else
         p-at-bottom = false.
   end.

END PROCEDURE.

PROCEDURE at-top :
/*------------------------------------------------------------------------------
  Purpose:     To test if we are at the top of the query.
  Parameters:  output p-at-top: logical
  Notes:
------------------------------------------------------------------------------*/
   define output parameter p-at-top as logical initial yes no-undo.

   if available(tt-browse) and valid-handle(h-query) then do:
      if tt-seq = 1 then
         p-at-top = true.
      else
         p-at-top = false.
   end.

END PROCEDURE.

PROCEDURE near-bottom :
/*------------------------------------------------------------------------------
  Purpose:     To test if we are near (within 1 CHUNK-SIZE of) the bottom of the query.
  Parameters:  output p-at-bottom: logical
  Notes:
------------------------------------------------------------------------------*/
   define output parameter p-near-bottom as logical initial no no-undo.

   if available(tt-browse) and valid-handle(h-query) then do:
      if tt-seq > 9999999 - {&CHUNK-SIZE} then
         p-near-bottom = true.
      else
         p-near-bottom = false.
  end.

END PROCEDURE.

PROCEDURE near-top :
/*------------------------------------------------------------------------------
  Purpose:     To test if we are near (within 1 CHUNK-SIZE of) the top of the query.
  Parameters:  output p-at-top: logical
  Notes:
------------------------------------------------------------------------------*/
   define output parameter p-near-top as logical initial no no-undo.

   if available(tt-browse) and valid-handle(h-query) then do:
      if tt-seq < {&CHUNK-SIZE} then
         p-near-top = true.
      else
         p-near-top = false.
   end.

END PROCEDURE.

PROCEDURE close-query :
/*------------------------------------------------------------------------------
  Purpose:     Close the query
  Parameters:  <none>
  Notes:
------------------------------------------------------------------------------*/

   close query {&BROWSE-NAME}.

END PROCEDURE.

PROCEDURE check-col-tab :
/*------------------------------------------------------------------------------
  Purpose:    Tab event was received from the cell overlay fill-in.
              Tab cursor to next indexable column or back up to the
              search field if it is at the end.
  Parameters: p-cell-handle: character  handle of the browse cell being overlayed
  Notes:      MOST OF THIS LOGIC IS PROVERSION 8 SPECIFIC!
------------------------------------------------------------------------------*/
   define input parameter p-cell-handle as character no-undo.
   define variable h-temp as handle no-undo.

   do with frame {&FRAME-NAME}:

      /*Proversion 8 specific logic*/
      &IF PROVERSION >= "8.0" &THEN

         /*Convert the parameter to a handle*/
         h-temp = widget-handle(p-cell-handle).

         /*Loop through the next indexable columns. If no more are available*/
         /*navigate back up to the search field.                            */
         do while true:

            h-temp = h-temp:next-column.

            if valid-handle(h-temp) then do:

               if lookup(h-temp:label,h-index-cb:list-items) > 0 then do:

                  &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
                     /*If next field is off of the screen*/
                     /*then do not overlay fill-in or an*/
                     /*error will occur in CHUI*/
                     if h-temp:column + h-temp:width-chars >=
                         (frame {&FRAME-NAME}:width-chars - 3)
                     then
                        run reset-search.
                  &ELSEIF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
                     if h-temp:column +
                        h-temp:width-chars >= 73
                     then
                        run reset-search.
                  &ENDIF

                  leave.

               end.

            end.

            else
               run reset-search.

         end.

         apply "ENTRY" to h-temp.
         run display-temp-table.

      &ENDIF

   end.

   /* "return error" is used to stop further processing of the
      return trigger.  It is not, strictly speaking, an actual error. */
   return error.

END PROCEDURE.

PROCEDURE reset-search :
/*-----------------------------------------------------------
    Purpose:     After last index field reset field, destroy
                 fill-in field, and display fields again.
    Parameters:  <none>
 -------------------------------------------------------------*/

   do with frame {&FRAME-NAME}:

      apply "ENTRY" to h-search-field.

      if valid-handle(h-cell) then
         run destroy-widget
         (input h-cell).

      h-cell = ?.

      &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
         if {&BROWSE-NAME}:refresh() then.
      &ENDIF

      run display-temp-table.

      if {&BROWSE-NAME}:deselect-rows() in frame {&FRAME-NAME} then.

      return error.

   end.

END PROCEDURE.

PROCEDURE display-temp-table :
/* -----------------------------------------------------------
  Purpose:     Redisplay previous cell - needed for CHUI
               repainting problem.
  Parameters:  <none>
 -------------------------------------------------------------*/

   if available tt-browse then do:

      display
         {&TT-Q-BROWSE-DEF-EXPR-1}
         {&TT-Q-BROWSE-DEF-EXPR-2}
         {&TT-Q-BROWSE-DEF-EXPR-3}
         {&TT-Q-BROWSE-DEF-EXPR-4}
         {&TT-Q-BROWSE-DEF-EXPR-5}
         {&TT-Q-BROWSE-DEF-EXPR-6}
         {&TT-Q-BROWSE-DEF-EXPR-7}
      with browse {&BROWSE-NAME}.

  end.

END PROCEDURE.

PROCEDURE create-search-field :
/* -----------------------------------------------------------
  Purpose:     Dynamically create the search-field fill-in.
  Parameters:
  Notes:       Each time the query is reopened, the fill-in must
               change to match the first column in the browse.
 -------------------------------------------------------------*/
   define variable c-lbl     as character format "x(60)" initial "" no-undo.
   define variable i-fld-cnt as integer                  initial 0  no-undo.
   define variable h-srt-fld as handle                              no-undo.
   define variable c-format  as character                           no-undo.
   define variable c-type    as character                           no-undo.
   define variable d-br-right-edge as decimal                       no-undo.
   define variable d-width-chars   as decimal                       no-undo.

   &IF INDEX(PROVERSION, "7.") > 0 &THEN
      define frame f-hidden
         {&TT-Q-BROWSE-DEF-EXPR-1}
            {&BR-K-COL-LABEL-1}  {&BR-COLUMN-LABEL-1}  {&BR-FIELD-FRMT-1}
         {&TT-Q-BROWSE-DEF-EXPR-2}
            {&BR-K-COL-LABEL-2}  {&BR-COLUMN-LABEL-2}  {&BR-FIELD-FRMT-2}
         {&TT-Q-BROWSE-DEF-EXPR-3}
            {&BR-K-COL-LABEL-3}  {&BR-COLUMN-LABEL-3}  {&BR-FIELD-FRMT-3}
         {&TT-Q-BROWSE-DEF-EXPR-4}
            {&BR-K-COL-LABEL-4}  {&BR-COLUMN-LABEL-4}  {&BR-FIELD-FRMT-4}
         {&TT-Q-BROWSE-DEF-EXPR-5}
            {&BR-K-COL-LABEL-5}  {&BR-COLUMN-LABEL-5}  {&BR-FIELD-FRMT-5}
         {&TT-Q-BROWSE-DEF-EXPR-6}
            {&BR-K-COL-LABEL-6}  {&BR-COLUMN-LABEL-6}  {&BR-FIELD-FRMT-6}
         {&TT-Q-BROWSE-DEF-EXPR-7}
            {&BR-K-COL-LABEL-7}  {&BR-COLUMN-LABEL-7}  {&BR-FIELD-FRMT-7}
      .
      &SCOPED-DEFINE CONTROL-PARENT frame f-hidden
   &ENDIF

   /* Allow for current 2.0 Webspeed and any near-term version changes.  This
      is an assumption, but the PROVERSION function returns "2.0 (WebSpeed)"
      from the server and "2.0" from the client (ie the Webspeed editor).  So
      we can't look for "Webspeed", and don't know how fast Progress will rev
      the Webspeed product. */
   &IF PROVERSION >= "8.0" or
       INDEX(PROVERSION, "2.") > 0 or
       INDEX(PROVERSION, "3.") > 0 or
       INDEX(PROVERSION, "4.") > 0
   &THEN
      &SCOPED-DEFINE CONTROL-PARENT browse {&BROWSE-NAME}
   &ENDIF

   /*Remove any previous dynamic cells & search field */
   if valid-handle(h-search-field) then
      run destroy-widget
         (input h-search-field).

   if valid-handle(h-cell) then
      run destroy-widget
         (input h-cell).

   assign
      h-cell = ?
      h-search-field = ?.

   CASE i-curr-index:

      WHEN 1 THEN do:
         assign
            c-lbl    = {&TT-Q-BROWSE-DEF-EXPR-1}:label in {&CONTROL-PARENT}
            c-format = entry(2, '{&BR-FIELD-FRMT-1}', chr(34))
            c-type   = {&TT-Q-BROWSE-DEF-EXPR-1}:data-type
            d-width-chars = {&TT-Q-BROWSE-DEF-EXPR-1}:width-chars.
      end.
      &IF {&Q-NUM-FILT-VALS} > 1 &THEN
      WHEN 2 THEN do:
         assign
            c-lbl    = {&TT-Q-BROWSE-DEF-EXPR-2}:label
            c-format = entry(2, '{&BR-FIELD-FRMT-2}', chr(34))
            c-type   = {&TT-Q-BROWSE-DEF-EXPR-2}:data-type
            d-width-chars = {&TT-Q-BROWSE-DEF-EXPR-2}:width-chars.
      end.
      &ENDIF
      &IF {&Q-NUM-FILT-VALS} > 2 &THEN
      WHEN 3 THEN do:
         assign
            c-lbl    = {&TT-Q-BROWSE-DEF-EXPR-3}:label
            c-format = entry(2, '{&BR-FIELD-FRMT-3}', chr(34))
            c-type   = {&TT-Q-BROWSE-DEF-EXPR-3}:data-type
            d-width-chars = {&TT-Q-BROWSE-DEF-EXPR-3}:width-chars.
      end.
      &ENDIF
      &IF {&Q-NUM-FILT-VALS} > 3 &THEN
      WHEN 4 THEN do:
         assign
            c-lbl    = {&TT-Q-BROWSE-DEF-EXPR-4}:label
            c-format = entry(2, '{&BR-FIELD-FRMT-4}', chr(34))
            c-type   = {&TT-Q-BROWSE-DEF-EXPR-4}:data-type
            d-width-chars = {&TT-Q-BROWSE-DEF-EXPR-4}:width-chars.
      end.
      &ENDIF
      &IF {&Q-NUM-FILT-VALS} > 4 &THEN
      WHEN 5 THEN do:
         assign
            c-lbl    = {&TT-Q-BROWSE-DEF-EXPR-5}:label
            c-format = entry(2, '{&BR-FIELD-FRMT-5}', chr(34))
            c-type   = {&TT-Q-BROWSE-DEF-EXPR-5}:data-type
            d-width-chars = {&TT-Q-BROWSE-DEF-EXPR-5}:width-chars.
      end.
      &ENDIF
      &IF {&Q-NUM-FILT-VALS} > 5 &THEN
      WHEN 6 THEN do:
         assign
            c-lbl    = {&TT-Q-BROWSE-DEF-EXPR-6}:label
            c-format = entry(2, '{&BR-FIELD-FRMT-6}', chr(34))
            c-type   = {&TT-Q-BROWSE-DEF-EXPR-6}:data-type
            d-width-chars = {&TT-Q-BROWSE-DEF-EXPR-6}:width-chars.
      end.
      &ENDIF
      &IF {&Q-NUM-FILT-VALS} > 6 &THEN
      WHEN 7 THEN do:
         assign
            c-lbl    = {&TT-Q-BROWSE-DEF-EXPR-7}:label
            c-format = entry(2, '{&BR-FIELD-FRMT-7}', chr(34))
            c-type   = {&TT-Q-BROWSE-DEF-EXPR-7}:data-type
            d-width-chars = {&TT-Q-BROWSE-DEF-EXPR-7}:width-chars.
      end.
      &ENDIF

   END CASE.

   d-br-right-edge = browse {&BROWSE-NAME}:COLUMN +
                     browse {&BROWSE-NAME}:WIDTH-CHARS.

   create fill-in h-search-field
   assign
      data-type = "character"
      format    = if c-type = "INTEGER" or
                     c-type = "DECIMAL"
                  then
                     "x(" + string(length(c-format,"raw")) + ")"
                  else c-format
      row       = 1
      frame     = frame {&FRAME-NAME}:handle
      x         = h-index-cb:x + h-index-cb:width-pixels + 5
      width-chars = min(d-width-chars, d-br-right-edge - h-search-field:column)
      sensitive = true
   triggers:
      on return       persistent run handle-return.
      on tab          persistent run position-col-focus.
      on cursor-up    persistent run fillin-cursor-up in THIS-PROCEDURE.
      on cursor-down  persistent run fillin-cursor-down in THIS-PROCEDURE.
      on page-up      persistent run fillin-page-up in THIS-PROCEDURE.
      on page-down    persistent run fillin-page-down in THIS-PROCEDURE.
      on scroll-right persistent run move-browse-right in this-procedure.
      on scroll-left  persistent run move-browse-left in this-procedure.
   end triggers.

   assign
      h-search-field:y = (browse {&BROWSE-NAME}:y - h-search-field:height-pixels) / 2
      h-search-field:screen-value = ""
      h-search-field:visible = true.

   return "".

END PROCEDURE.

PROCEDURE create-cell-overlay :
/* -----------------------------------------------------------
    Purpose:     Dynamically create the search-field fill-in.
    Parameters:
    Notes:       Each time the query is reopened, the fill-in must
                 change to match the first column in the browse.
  -------------------------------------------------------------*/

   if available(tt-browse) and valid-handle(h-query) then do:

      &IF INDEX(PROVERSION, "7.") > 0 &THEN
         define frame f-hidden
            {&TT-Q-BROWSE-DEF-EXPR-1}
               {&BR-K-COL-LABEL-1}  {&BR-COLUMN-LABEL-1}  {&BR-FIELD-FRMT-1}
            {&TT-Q-BROWSE-DEF-EXPR-2}
               {&BR-K-COL-LABEL-2}  {&BR-COLUMN-LABEL-2}  {&BR-FIELD-FRMT-2}
            {&TT-Q-BROWSE-DEF-EXPR-3}
               {&BR-K-COL-LABEL-3}  {&BR-COLUMN-LABEL-3}  {&BR-FIELD-FRMT-3}
            {&TT-Q-BROWSE-DEF-EXPR-4}
               {&BR-K-COL-LABEL-4}  {&BR-COLUMN-LABEL-4}  {&BR-FIELD-FRMT-4}
            {&TT-Q-BROWSE-DEF-EXPR-5}
               {&BR-K-COL-LABEL-5}  {&BR-COLUMN-LABEL-5}  {&BR-FIELD-FRMT-5}
            {&TT-Q-BROWSE-DEF-EXPR-6}
               {&BR-K-COL-LABEL-6}  {&BR-COLUMN-LABEL-6}  {&BR-FIELD-FRMT-6}
            {&TT-Q-BROWSE-DEF-EXPR-7}
               {&BR-K-COL-LABEL-7}  {&BR-COLUMN-LABEL-7}  {&BR-FIELD-FRMT-7}
            .
         &SCOPED-DEFINE CONTROL-PARENT frame f-hidden
      &ENDIF

      /* Allow for current 2.0 Webspeed and any near-term version changes.  This
         is an assumption, but the PROVERSION function returns "2.0 (WebSpeed)"
         from the server and "2.0" from the client (ie the Webspeed editor).  So
         we can't look for "Webspeed", and don't know how fast Progress will rev
         the Webspeed product. */
      &IF PROVERSION >= "8.0" or
          INDEX(PROVERSION, "2.") > 0 or
          INDEX(PROVERSION, "3.") > 0 or
          INDEX(PROVERSION, "4.") > 0
      &THEN
         &SCOPED-DEFINE CONTROL-PARENT browse {&BROWSE-NAME}
      &ENDIF

      if valid-handle(h-cell) then
         run destroy-widget
            (input h-cell).

      h-cell = ?.

      create fill-in h-cell
      assign
         data-type    = self:data-type
         name         = self:name
         frame        = frame {&FRAME-NAME}:handle
         width-chars  = self:width-chars
         sensitive    = true
         auto-zap     = true
         visible      = if self:x < 0 then false else true
         private-data = string(self)
         &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
            row       = ({&BROWSE-NAME}:row    + self:row)    - 1
            column    = ({&BROWSE-NAME}:column + self:column) - 1
         &ELSE
            x         = {&BROWSE-NAME}:x + self:x
            y         = {&BROWSE-NAME}:Y + self:y
         &ENDIF
      triggers:
         on return, mouse-select-dblclick
            persistent run go-back in THIS-PROCEDURE.
         on tab
            persistent run check-col-tab in THIS-PROCEDURE (input h-cell:private-data).
         on cursor-up
            persistent run fillin-cursor-up in THIS-PROCEDURE.
         on cursor-down
            persistent run fillin-cursor-down in THIS-PROCEDURE.
         on page-up
            persistent run fillin-page-up in THIS-PROCEDURE.
         on page-down
            persistent run fillin-page-down in THIS-PROCEDURE.
         on go, mouse-extend-dblclick
            persistent run handle-return.
      end triggers.

      /*If negative then search for next index column*/
      if self:x < 0 then
         run check-col-tab in THIS-PROCEDURE
            (input h-cell:private-data).

      /*Ensure it is on top, move-to-top only works in GUI*/
      &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
         if valid-handle(h-cell) then
            if h-cell:move-to-top() then .
      &ENDIF

      /*Keep the combo-box in sync*/
      if valid-handle(h-index-cb) then do:

         /*Changed to always refresh combo box (needed for CHUI)*/
         /*if h-index-cb:screen-value <> self:label then do:    */
         h-index-cb:screen-value = trim(self:label).

         if not(h-index-cb:lookup(h-index-cb:screen-value) <= 0
               or h-index-cb:lookup(h-index-cb:screen-value) >
               h-index-cb:num-items)
         then
            i-curr-index =
               integer(entry(h-index-cb:lookup(h-index-cb:screen-value),
                             h-index-cb:private-data)).

      end.

      /*Set the format*/
      CASE i-curr-index:
         WHEN 1 THEN
            h-cell:format = entry(2, '{&BR-FIELD-FRMT-1}', chr(34)).
         &IF {&Q-NUM-FILT-VALS} > 1 &THEN
         WHEN 2 THEN
            h-cell:format = entry(2, '{&BR-FIELD-FRMT-2}', chr(34)).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 2 &THEN
         WHEN 3 THEN
            h-cell:format = entry(2, '{&BR-FIELD-FRMT-3}', chr(34)).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 3 &THEN
         WHEN 4 THEN
            h-cell:format = entry(2, '{&BR-FIELD-FRMT-4}', chr(34)).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 4 &THEN
         WHEN 5 THEN
            h-cell:format = entry(2, '{&BR-FIELD-FRMT-5}', chr(34)).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 5 &THEN
         WHEN 6 THEN
            h-cell:format = entry(2, '{&BR-FIELD-FRMT-6}', chr(34)).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 6 &THEN
         WHEN 7 THEN
            h-cell:format = entry(2, '{&BR-FIELD-FRMT-7}', chr(34)).
         &ENDIF
      END CASE.

      /*Screen values must be set AFTER the format*/
      /*put browse field value into the fill-in overlay*/
      h-cell:screen-value = self:screen-value.

      if valid-handle(h-search-field)
      then do:

         h-search-field:screen-value = "".                   /*clear*/

         if h-cell:data-type = "INTEGER" or
            h-cell:data-type = "DECIMAL"
         then do:
            h-search-field:format = "x(" +
                                    string(length(h-cell:format,"raw")) + ")".
         end.
         else
            h-search-field:format = h-cell:format.      /*reformat*/

         /* THE CHARACTER WIDTH OF SEARCH FIELD IS SET TO EITHER THE     */
         /* CHARACTER WIDTH OF THE CURRENTLY ACTIVE COLUMN IN BROWSE OR  */
         /* THE VALUE OF EXPRESSION (BROWSE WIDTH - SEARCH FIELD WIDTH)  */
         /* WHICHEVER  VALUE IS SMALLER.                                 */
         h-search-field:width-chars = min(h-cell:width-chars,
                            (browse {&BROWSE-NAME}:width-chars -
                             h-index-cb:width-chars)).

         /*Put new value into search field*/
         /*h-search-field:screen-value = h-cell:screen-value.*/
         h-search-field:screen-value = string(h-cell:screen-value).

         /* IF CURRENT CELL IS OF DATA-TYPE DATE & SEARCH FIELDS SCREENVALUE */
         /* MATCHES "/  /"  i.e. LENGTH OF 4                                 */
         if h-cell:data-type = "DATE" and
            length(string(trim(h-search-field:screen-value))) = 4
         then
            h-search-field:screen-value = ?.

      end.

      if {&BROWSE-NAME}:select-focused-row()then.

      apply "ENTRY" to h-cell.

   end.

   return "".

END PROCEDURE.

PROCEDURE create-tt :
/*------------------------------------------------------------------------------
  Purpose:     To create and populate a record in the temp table.
  Parameters:
  Notes:       This is where we need to cast database variables according
               to the formats or variable expressions defined for the browse
               (e.g. Logicals as character).
------------------------------------------------------------------------------*/
   define buffer b-1 for {&Q-FIRST-TABLE}.
   &IF {&Q-NUM-TABLES} > 1 &THEN
   define buffer b-2 for {&Q-2ND-TABLE}.
   &ENDIF
   &IF {&Q-NUM-TABLES} > 2 &THEN
   define buffer b-3 for {&Q-3RD-TABLE}.
   &ENDIF
   &IF {&Q-NUM-TABLES} > 3 &THEN
   define buffer b-4 for {&Q-4TH-TABLE}.
   &ENDIF

   run get-current-record in h-query
      (BUFFER b-1
       &IF {&Q-NUM-TABLES} > 1 &THEN , BUFFER b-2 &ENDIF
       &IF {&Q-NUM-TABLES} > 2 &THEN , BUFFER b-3 &ENDIF
       &IF {&Q-NUM-TABLES} > 3 &THEN , BUFFER b-4 &ENDIF ).

   do transaction on error undo, leave:

      /* Bump primary index sequence */
      i-tt-seq = i-tt-seq + i-tt-seq-inc.

      create tt-browse.
      assign
         tt-seq                 = i-tt-seq
         tt-recid               = recid(b-1).

      assign
         {&TT-Q-BROWSE-DEF-EXPR-1}  = {&Q-BROWSE-DISP-EXPR-1}
      no-error.

      &IF {&Q-NUM-FILT-VALS} > 1 &THEN
      assign
         {&TT-Q-BROWSE-DEF-EXPR-2}  = {&Q-BROWSE-DISP-EXPR-2}
      no-error.
      &ENDIF

      &IF {&Q-NUM-FILT-VALS} > 2 &THEN
      assign
         {&TT-Q-BROWSE-DEF-EXPR-3}  = {&Q-BROWSE-DISP-EXPR-3}
      no-error.
      &ENDIF

      &IF {&Q-NUM-FILT-VALS} > 3 &THEN
      assign
         {&TT-Q-BROWSE-DEF-EXPR-4}  = {&Q-BROWSE-DISP-EXPR-4}
      no-error.
      &ENDIF

      &IF {&Q-NUM-FILT-VALS} > 4 &THEN
      assign
         {&TT-Q-BROWSE-DEF-EXPR-5}  = {&Q-BROWSE-DISP-EXPR-5}
      no-error.
      &ENDIF

      &IF {&Q-NUM-FILT-VALS} > 5 &THEN
      assign
         {&TT-Q-BROWSE-DEF-EXPR-6}  = {&Q-BROWSE-DISP-EXPR-6}
      no-error.
      &ENDIF

      &IF {&Q-NUM-FILT-VALS} > 6 &THEN
      assign
         {&TT-Q-BROWSE-DEF-EXPR-7}  = {&Q-BROWSE-DISP-EXPR-7}
      no-error.
      &ENDIF

   end.

END PROCEDURE.

PROCEDURE cell-entry :
/*------------------------------------------------------------------------------
  Purpose:     Perform necessary default behavior for setting focus in cell
  Parameters:  None.
------------------------------------------------------------------------------*/

   /*Set the screen value of the combo box & index fill-in to reflect the */
   /*cell's properties and value.                                         */
   run create-cell-overlay.

END PROCEDURE.

PROCEDURE determine-column :
/*------------------------------------------------------------------------------
  Purpose:     Return the index of the current cell column
  Parameters:  p-column
------------------------------------------------------------------------------*/
   define output parameter p-column as integer no-undo.

   if valid-handle(h-index-cb) then
      p-column = h-index-cb:lookup(h-index-cb:screen-value).

END PROCEDURE.

PROCEDURE destroy-widget :
/*------------------------------------------------------------------------------
  Purpose:     Destroy the dynamic cell overlay fill-in
  Parameters:  p-handle
------------------------------------------------------------------------------*/
   define input parameter p-handle as handle no-undo.

   /*Delete the widget and set the handle to ?*/
   if valid-handle(p-handle) then do:

      if p-handle:dynamic then do:
         p-handle:visible = false.
         delete widget p-handle.
         p-handle = ?.
      end.
      else
         p-handle:visible = false.
   end.

END PROCEDURE.

PROCEDURE disable_UI :
/*------------------------------------------------------------------------------
  Purpose:     DISABLE the User Interface
  Parameters:  <none>
  Notes:       Here we clean-up the user-interface by deleting
               dynamic widgets we have created and/or hide
               frames.  This procedure is usually called when
               we are ready to "clean-up" after running.
------------------------------------------------------------------------------*/
   /* Hide all frames. */
   hide frame Dialog-Frame.

END PROCEDURE.

PROCEDURE enable_UI :
/*------------------------------------------------------------------------------
  Purpose:     ENABLE the User Interface
  Parameters:  <none>
  Notes:       Here we display/view/enable the widgets in the
               user-interface.  In addition, OPEN all queries
               associated with each frame and BROWSE.
               These statements here are based on the "Other
               Settings" section of the widget Property Sheets.
------------------------------------------------------------------------------*/
   define variable c-initial-value as character no-undo.

   run q-font-size.

   /*Don't readjust frame, already done in q-adjust-widgets*/
   enable
      c-index-cb
      BROWSE-2
   with frame Dialog-Frame.

   view frame Dialog-Frame.

   i-curr-index = integer(entry(1, h-index-cb:private-data)).
   h-index-cb:screen-value = entry(1, h-index-cb:list-items).
   h-index-cb:sensitive    = h-index-cb:num-items > 1.

   if valid-handle(global-drill-handle) then
      c-initial-value = global-drill-handle:screen-value.

   if {&Q-DRAG-BACK-COL} > 1 then
      c-initial-value = "".

   run get-new-query
      (input i-curr-index,
       input c-initial-value).

   if return-value = "ERROR" then
      return error "ERROR".

   apply "ENTRY" to h-search-field.

END PROCEDURE.

PROCEDURE fillin-cursor-down :
/*------------------------------------------------------------------------------
  Purpose:     Make the fill-in fire the cursor down trigger for the browse
  Parameters:  <none>
------------------------------------------------------------------------------*/
   define variable i-column as integer no-undo.

   do with frame {&FRAME-NAME}:

      if valid-handle(h-cell) then do:
         run destroy-widget in THIS-PROCEDURE (h-cell).
         h-cell = ?.
      end.

      apply "CURSOR-DOWN" to browse {&BROWSE-NAME}.

      /*   TO CAPTURE LAST EVENT LABEL               */
      assign c-prev-event = last-event:label.

   end.

   return error.

END PROCEDURE.

PROCEDURE fillin-cursor-up :
/*------------------------------------------------------------------------------
  Purpose:     Make the fill-in fire the cursor up trigger for the browse
  Parameters:  <none>
------------------------------------------------------------------------------*/
   do with frame {&FRAME-NAME}:

      if valid-handle(h-cell) then do:
         run destroy-widget in THIS-PROCEDURE (h-cell).
         h-cell = ?.
      end.

      apply "CURSOR-UP" to browse {&BROWSE-NAME}.

   end.

   return error.

END PROCEDURE.

PROCEDURE move-browse-right :
/* -----------------------------------------------------------
    Purpose:     Scroll horizontally right in lookup.
    Parameters:
    Notes:       Each time scroll-right key is pressed, lookup
                 should scroll to right side.
-------------------------------------------------------------*/

   apply "CURSOR-RIGHT" to browse {&BROWSE-NAME}.
   return "".

END PROCEDURE.

PROCEDURE move-browse-left :
/* -----------------------------------------------------------
    Purpose:     Scroll horizontally left in lookup.
    Parameters:
    Notes:       Each time scroll-left key is pressed, lookup
                 should scroll to left side.
 -------------------------------------------------------------*/

   apply "CURSOR-LEFT" to browse {&BROWSE-NAME}.
   return "".

END PROCEDURE.

PROCEDURE fillin-page-down :
/*------------------------------------------------------------------------------
  Purpose:     Make the fill-in fire the page down trigger for the browse
  Parameters:  <none>
------------------------------------------------------------------------------*/
   do with frame {&FRAME-NAME}:

      if valid-handle(h-cell) then do:
         run destroy-widget in THIS-PROCEDURE (h-cell).
         h-cell = ?.
      end.

      apply "PAGE-DOWN" to browse {&BROWSE-NAME}.

      /* CODE TO TRIGGER OFF-END IN CHUI     */
      &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
         get next {&BROWSE-NAME}.
         if (query-off-end("{&BROWSE-NAME}"))
         or ({&BROWSE-NAME}:down <> {&BROWSE-NAME}:num-iterations) then
            apply "OFF-END" to browse {&BROWSE-NAME}.
      &ENDIF

   end.

   return error.

END PROCEDURE.

PROCEDURE fillin-page-up :
/*------------------------------------------------------------------------------
  Purpose:     Make the fill-in fire the page up trigger for the browse
  Parameters:  <none>
------------------------------------------------------------------------------*/
   do with frame {&FRAME-NAME}:

      if valid-handle(h-cell) then do:
         run destroy-widget in THIS-PROCEDURE (h-cell).
         h-cell = ?.
      end.

      apply "PAGE-UP" to browse {&BROWSE-NAME}.

   end.

   return error.

END PROCEDURE.

PROCEDURE find-recid-and-screen-value :
/*------------------------------------------------------------------------------
  Purpose:     Find the record id and Screen value for the current row
  Parameters:  <none>
------------------------------------------------------------------------------*/

   if available(tt-browse) then do:

      r-key-recno = tt-browse.tt-recid.

      CASE i-curr-index :

         when 1 then
            h-search-field:screen-value = string({&TT-Q-BROWSE-DEF-EXPR-1}).
         &IF {&Q-NUM-FILT-VALS} > 1 &THEN
         when 2 then
            h-search-field:screen-value = string({&TT-Q-BROWSE-DEF-EXPR-2}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 2 &THEN
         when 3 then
            h-search-field:screen-value = string({&TT-Q-BROWSE-DEF-EXPR-3}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 3 &THEN
         when 4 then
            h-search-field:screen-value = string({&TT-Q-BROWSE-DEF-EXPR-4}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 4 &THEN
         when 5 then
            h-search-field:screen-value = string({&TT-Q-BROWSE-DEF-EXPR-5}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 5 &THEN
         when 6 then
            h-search-field:screen-value = string({&TT-Q-BROWSE-DEF-EXPR-6}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 6 &THEN
         when 7 then
            h-search-field:screen-value = string({&TT-Q-BROWSE-DEF-EXPR-7}).
         &ENDIF

      END CASE.

    end.

END PROCEDURE.

PROCEDURE flush-temp-table :
/*------------------------------------------------------------------------------
  Purpose:     To purge the temp table of all data.
  Parameters:  <none>
  Notes:
------------------------------------------------------------------------------*/

   run close-query.

   for each tt-browse exclusive-lock:
      delete tt-browse.
  end.

END PROCEDURE.

PROCEDURE get-first-chunk :
/*------------------------------------------------------------------------------
  Purpose:     To grab the first chunk of data from a query.
  Parameters:  <none>
  Notes:       This procedure purges any data that might already exist in the
               temp table, opens the query, and positions the browse to the
               top of the temp table.
------------------------------------------------------------------------------*/

   run flush-temp-table.

   assign
      i-tt-seq-inc = 1 /* increment tt index when starting at end of query */
      i-tt-seq     = 0. /* start index at nominal initial value */

   run get-rows
      (input "FIRST").

   run re-open-tt-query.

END PROCEDURE.

PROCEDURE get-last-chunk :
/*------------------------------------------------------------------------------
  Purpose:     To grab the last chunk of data from a query.
  Parameters:  <none>
  Notes:       This procedure purges any data that might already exist in the
               temp table, opens the query, and positions the browse to the
               top of the temp table.
------------------------------------------------------------------------------*/

   run flush-temp-table.


   assign
      i-tt-seq-inc = -1 /* decrement tt index when starting at end of query */
      i-tt-seq     = 9999999. /* start index at large initial value */

   run get-rows
      (input "LAST").

   run re-open-tt-query.

END PROCEDURE.

PROCEDURE get-new-query :
/*------------------------------------------------------------------------------
  Purpose:     To get a new query from the manager
  Parameters:  <none>
  Notes:
------------------------------------------------------------------------------*/
   define input parameter p-index as integer   no-undo.
   define input parameter p-start as character no-undo.

   define variable h-new-query as handle no-undo.
   define variable l-ok as logical     no-undo.

   do with frame {&FRAME-NAME}:
      c-scroll-msg:screen-value = "".
   end.

   /*Change the cursor to the hour-glass during processing*/
   {gprun.i 'gpcursor.p' "(input 'wait')"}

   run create-search-field.

   if valid-handle(h-cell) then
      run destroy-widget
         (input h-cell).

   h-cell = ?.


   if "{&WINDOW-SYSTEM}" <> "TTY" and num-results("BROWSE-2") <> 0 then do:
      l-ok = browse {&BROWSE-NAME}:move-column(1, i-prev-col).
      l-ok = browse {&BROWSE-NAME}:move-column(p-index, 1).
      i-prev-col = p-index.
   end.

   run get-new-lookup-query in h-manager
      (input  c-browse-name,
       input  p-index,
       input  p-start,
       output h-new-query).

   /*Change cursor back to pointer to ready screen for input*/
   {gprun.i 'gpcursor.p' "(input '')}

   if valid-handle(h-new-query) then
      run set-query-handle
         (input h-new-query).
   else
      return error "ERROR".

   return "".

END PROCEDURE.

PROCEDURE get-next-chunk :
/*------------------------------------------------------------------------------
  Purpose:     To grab the next chunk of data from a query.
  Parameters:  <none>
  Notes:       This procedure continues reading data from an already open query.
               If a lower level routine gets to the end of the query, it reports
               an error and processing stops.  Otherwise, data are added to the
               temp table and then the browse is repositioned.
------------------------------------------------------------------------------*/

   do on error undo, leave:

      run get-rows
         (input "NEXT").

      run reposition-tt
         (input r-tempb-id,
          input "NEXT").

      return "".

   end.

   /* We get here because we've advanced beyond the end of the file */
   run get-last-chunk.

END PROCEDURE.

PROCEDURE get-prev-chunk :
/*------------------------------------------------------------------------------
  Purpose:     To grab the previous chunk of data from a query.
  Parameters:  <none>
  Notes:       This procedure reads data "backwards" from an already open query.
               If a lower level routine gets to the top of the query, it reports
               an error and processing stops.  Otherwise, data are added to the
               temp table and then the browse is repositioned.
------------------------------------------------------------------------------*/

   do on error undo, leave:

      run get-rows
         (input "PREV").

      run reposition-tt
         (input r-tempb-id,
          input "PREV").

      return "".

   end.

   /* We are here because we've backed up to the top of the file */
   run get-first-chunk.

END PROCEDURE.

PROCEDURE get-rows :
/*------------------------------------------------------------------------------
  Purpose:     To get the rows that constitute the right "chunk" for the temp table
  Parameters:  p-direction: a character string that indicates which direction
                            the reading is going in.
  Notes:       The key to this routine is that it stops processing after a fixed
               (and small) number of records.
------------------------------------------------------------------------------*/
   define input parameter p-direction as character no-undo.

   define variable i as integer no-undo.
   define variable l-buf-available as logical no-undo.

   CASE p-direction:

      when "FIRST" then do:
         run get-first in h-query.
      end.

      when "NEXT"  then do:
         do on error undo, return error:   /* watch for beyond end of query */
            run get-next in h-query.
         end.
      end.

      when "PREV"  then do:
         do on error undo, return error:   /* watch for before top of query */
            run get-prev in h-query.
         end.
      end.

      when "LAST"  then do:
         run get-last in h-query.
      end.

   END CASE.

   run buf-available in h-query
      (output l-buf-available).

   get-loop:
   do while l-buf-available on error undo, return error:

      run create-tt.

      i = i + 1.

      if i >= {&CHUNK-SIZE} then
         leave get-loop.

      CASE p-direction:

         when "FIRST" or when "NEXT" then
         do on error undo, return error "LAST":
            run get-next in h-query.
         end.

         when "PREV"  or when "LAST" then
         do on error undo, return error "TOP":
            run get-prev in h-query.
         end.

      END CASE.

      run buf-available in h-query
         (output l-buf-available).

   end.

END PROCEDURE.

PROCEDURE get-srch-msg :
/*------------------------------------------------------------------------------
  Purpose:     Get the search message which will be displayed when getting
               a new chunk.
  Parameters:  <none>
------------------------------------------------------------------------------*/

   run get-browse-attributes in h-manager
      (input "{&BR-NAME}",
       input  "SRCH-MSG",
       output c-srch-msg).

END PROCEDURE.

PROCEDURE go-back :
/*------------------------------------------------------------------------------
  Purpose:     Return to the calling program & return the drag-back value
  Parameters:  <none>
------------------------------------------------------------------------------*/

   do with frame {&FRAME-NAME}:
      apply "return" to browse {&BROWSE-NAME}.
      return error.   /* "return error" is used to stop further processing
                         of the return trigger.  It is not, strictly speaking,
                         an actual error. */
   end.

END PROCEDURE.

PROCEDURE handle-return :
/* -----------------------------------------------------------
  Purpose:     to deal with a carriage return entered in the search field
  Parameters:  <none>
  Notes:
-------------------------------------------------------------*/

   run start-new-query
      (input self:screen-value).

   return error.  /* "return error" is used to stop further processing
                     of the go trigger.  It is not, strictly speaking,
                     an actual error. */

END PROCEDURE.

PROCEDURE position-col-focus :
/* -----------------------------------------------------------
  Purpose:     to position focus to the correct cell as
               identified by the sort combo-box
  Parameters:  <none>
  Notes:       MOST OF THIS LOGIC IS PROVERSION 8 SPECIFIC!
------------------------------------------------------------*/
   define variable h-temp as handle no-undo.

   do with frame {&FRAME-NAME}:

      if available(tt-browse) and valid-handle(h-query) then do:

         /*Proversion 8 specific logic*/
         &IF PROVERSION >= "8.0" &THEN

            /*Convert the parameter to a handle*/
             h-temp = {&BROWSE-NAME}:current-column.

             if valid-handle(h-temp) then do:

                /* Loop through the next indexable columns.*/
                /* If no more are available                */
                /* navigate back up to the search field.   */
                do while valid-handle(h-temp):
                   if h-temp:label = h-index-cb:screen-value then leave.
                   h-temp = h-temp:prev-column.
                end.

                if not valid-handle(h-temp) then
                   h-temp = {&BROWSE-NAME}:current-column.

                /* Need to apply focus first to browse before it
                   will go to cell focus*/
                if {&BROWSE-NAME}:select-row(1) in frame {&FRAME-NAME} then.

                /*Now focus the cell*/
                apply "ENTRY" to h-temp.

                apply "end-search" to browse {&BROWSE-NAME}.

                l-search-ok = false.

             end.

          &ELSE
             /*Proversion 7 support*/
             apply "ENTRY" to browse {&BROWSE-NAME}.
          &ENDIF

       end.

    end.

END PROCEDURE.

PROCEDURE start-new-query :
/* -----------------------------------------------------------
  Purpose:     To start a new query based on user input
  Parameters:  input: p-start-value: value to begin query at
  Notes:
-------------------------------------------------------------*/
   define input parameter p-start-value as character no-undo.

   /*This gets focus back to a central point*/
   if valid-handle(h-search-field) then
      apply "ENTRY" to h-search-field.

   run get-new-query
      (input i-curr-index,
       input p-start-value).

   if return-value = "ERROR" then
      return error.

   run position-col-focus.

   return "".

END PROCEDURE.

PROCEDURE q-adjust-widgets :
/* -----------------------------------------------------------
  Purpose:     This method is executed once to adjust the screen
                layout of the widgets.

  Parameters:  <none>
  Notes:
  Method Type: Private
-------------------------------------------------------------*/
   define variable frame-wid    as decimal       no-undo.
   define variable widest-width as decimal       no-undo.
   define variable i            as integer       no-undo.
   define variable i-fld        as integer       no-undo.
   define variable i-wid        as integer       no-undo.
   define variable l-tmp        as logical       no-undo.
   define variable c-tmp        as character     no-undo.
   define variable c-list       as character     no-undo.

   assign
      frame {&FRAME-NAME}:SCROLLABLE = YES
      frame-wid = max(browse {&BROWSE-NAME}:WIDTH-CHARS + 3, widest-width)
      c-scroll-msg:ROW =
         browse {&BROWSE-NAME}:ROW + browse {&BROWSE-NAME}:HEIGHT-CHARS
      frame {&FRAME-NAME}:WIDTH-CHARS  =
         min(max(frame-wid,c-scroll-msg:WIDTH-CHARS + 3), 80)
      frame {&FRAME-NAME}:HEIGHT-CHARS = c-scroll-msg:ROW + 2 - 8
      frame {&FRAME-NAME}:VIRTUAL-WIDTH-CHARS  =
         max(frame {&FRAME-NAME}:WIDTH-CHARS,
             browse {&BROWSE-NAME}:WIDTH-CHARS + 3)
      frame {&FRAME-NAME}:VIRTUAL-HEIGHT-CHARS =
         frame {&FRAME-NAME}:HEIGHT-CHARS

      /* IF THE VALUE OBTAINED FOR STARTING COLUMN of BROWSE   */
      /* IS GREATER THAN DEFAULT VALUE THEN IT GETS SET TO THE */
      /* DEFAULT VALUE                                         */
      browse {&BROWSE-NAME}:COLUMN =
         min(((frame {&FRAME-NAME}:VIRTUAL-WIDTH-CHARS -
               browse {&BROWSE-NAME}:WIDTH-CHARS) / 2),
               browse {&BROWSE-NAME}:COLUMN)

      h-index-cb   = c-index-cb:handle in frame {&FRAME-NAME}
      h-index-cb:x = browse {&BROWSE-NAME}:x
      h-index-cb:y =
         &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
            (browse {&BROWSE-NAME}:y - 8) / 2
         &ELSE
           (browse {&BROWSE-NAME}:y - h-index-cb:height-pixels) / 2
         &ENDIF
      h-index-cb:private-data =
         if "{&BR-INDEXED-FIELDS}" = "" then "1" else "{&BR-INDEXED-FIELDS}".


   &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
       if valid-handle(h-index-cb) then
           if h-index-cb:move-to-top() then .
   &ENDIF

   c-list = fill(",", num-entries(h-index-cb:private-data) - 1).

   do i = 1 to num-entries(h-index-cb:private-data):

      i-fld = integer(entry(i,h-index-cb:private-data)).

      CASE i-fld:

         WHEN 1 THEN do:
            /* IF EXTERNALIZED LABELS ARE USED GET LABELS FROM  */
            /* BROWSE COLUMN LABELS. OTHERWISE FROM BROWSE DEF. */
            if dynamic-function('getTranslateFramesFlag' in h-label) then do:
               run getBrowseColumnLabel
                  (input i-fld,
                   output c-tmp).
            end.
            else
               c-tmp = {&BR-COLUMN-LABEL-1}.
         end.

         &IF {&Q-NUM-FILT-VALS} > 1 &THEN
         WHEN 2 THEN do:
            if dynamic-function('getTranslateFramesFlag' in h-label) then do:
               run getBrowseColumnLabel
                  (input i-fld,
                   output c-tmp).
            end.
            else
               c-tmp = {&BR-COLUMN-LABEL-2}.
         end.
         &ENDIF

         &IF {&Q-NUM-FILT-VALS} > 2 &THEN
         WHEN 3 THEN do:
            if dynamic-function('getTranslateFramesFlag' in h-label) then do:
               run getBrowseColumnLabel
                  (input i-fld,
                   output c-tmp).
            end.
            else
               c-tmp = {&BR-COLUMN-LABEL-3}.
         end.
         &ENDIF

         &IF {&Q-NUM-FILT-VALS} > 3 &THEN
         WHEN 4 THEN do:
            if dynamic-function('getTranslateFramesFlag' in h-label) then do:
               run getBrowseColumnLabel
                  (input i-fld,
                   output c-tmp).
            end.
            else
               c-tmp = {&BR-COLUMN-LABEL-4}.
         end.
         &ENDIF

         &IF {&Q-NUM-FILT-VALS} > 4 &THEN
         WHEN 5 THEN do:
            if dynamic-function('getTranslateFramesFlag' in h-label) then do:
               run getBrowseColumnLabel
                  (input i-fld,
                   output c-tmp).
            end.
            else
               c-tmp = {&BR-COLUMN-LABEL-5}.
         end.
         &ENDIF

         &IF {&Q-NUM-FILT-VALS} > 5 &THEN
         WHEN 6 THEN do:
            if dynamic-function('getTranslateFramesFlag' in h-label) then do:
               run getBrowseColumnLabel
                  (input i-fld,
                   output c-tmp).
            end.
            else
               c-tmp = {&BR-COLUMN-LABEL-6}.
         end.
         &ENDIF

         &IF {&Q-NUM-FILT-VALS} > 6 &THEN
         WHEN 7 THEN do:
            if dynamic-function('getTranslateFramesFlag' in h-label) then do:
               run getBrowseColumnLabel
                  (input i-fld,
                   output c-tmp).
            end.
            else
               c-tmp = {&BR-COLUMN-LABEL-7}.
         end.
         &ENDIF

      END CASE.

      entry(i, c-list) = c-tmp.

      &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
         i-wid = max(i-wid, length(c-tmp,"raw")).
      &ELSE
         i-wid = max(i-wid, font-table:GET-TEXT-WIDTH-PIXELS(c-tmp)).
      &ENDIF

   end.

   h-index-cb:list-items   = c-list.

   &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
      h-index-cb:width-chars  = i-wid + 4.  /* leave room for [V] */
   &ELSE
      h-index-cb:width-pixels = i-wid + 25. /* leave room for button */
   &ENDIF

   frame {&FRAME-NAME}:SCROLLABLE =
      frame {&FRAME-NAME}:VIRTUAL-WIDTH-CHARS > frame {&FRAME-NAME}:WIDTH-CHARS.

   return "".

END PROCEDURE.

PROCEDURE q-font-size :
/*------------------------------------------------------------------------------
  Purpose:     To adjust the vertical dimension of the frame and its widgets
  Parameters:  <none>
  Notes:
------------------------------------------------------------------------------*/
   define variable i-font1-ht-pixels as integer no-undo.
   define variable i-fontd-ht-pixels as integer no-undo.
   define variable h-temp1           as widget-handle no-undo.
   define variable h-temp2           as widget-handle no-undo.

   /* Scale height of frame & buttons for different resolutions-font sets */
   assign
      i-font1-ht-pixels = font-table:get-text-height-pixels(1)
      i-fontd-ht-pixels = font-table:get-text-height-pixels().

   frame {&FRAME-NAME}:height-pixels =
      truncate((frame {&FRAME-NAME}:height-pixels * i-font1-ht-pixels)
        / i-fontd-ht-pixels, 0).

   h-temp1 = frame {&FRAME-NAME}:first-child. /* first frame group */
   h-temp2 = h-temp1:first-child.             /* first widget */

   repeat while h-temp2 <> ?:
      &IF "{&WINDOW-SYSTEM}" <> "TTY" &THEN
         if h-temp2:type <> "COMBO-BOX" and
            h-temp2:type <> "BROWSE"
         then
            h-temp2:height-pixels =
               truncate((h-temp2:height-pixels * i-font1-ht-pixels)
                  / i-fontd-ht-pixels, 0).
      &ENDIF
      h-temp2 = h-temp2:next-sibling.
   end.

   return "".

END PROCEDURE.

PROCEDURE q-ok :
/* -----------------------------------------------------------
  Purpose:     This method is executed on an affirmative action
                by the user. (dbl-click, OK, or Return)
  Parameters:  <none>
  Notes:
  Method Type: Private
-------------------------------------------------------------*/
   define variable c-value as character no-undo.

   /*Prevent progress error due to a blank browser*/
   if available tt-browse then do:

      apply "ENTRY" to browse {&BROWSE-NAME}.

      CASE {&Q-DRAG-BACK-COL}:
         when 1 then c-value = string(tt-browse.{&TT-Q-BROWSE-DEF-EXPR-1}).
         &IF {&Q-NUM-FILT-VALS} > 1 &THEN
         when 2 then c-value = string(tt-browse.{&TT-Q-BROWSE-DEF-EXPR-2}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 2 &THEN
         when 3 then c-value = string(tt-browse.{&TT-Q-BROWSE-DEF-EXPR-3}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 3 &THEN
         when 4 then c-value = string(tt-browse.{&TT-Q-BROWSE-DEF-EXPR-4}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 4 &THEN
         when 5 then c-value = string(tt-browse.{&TT-Q-BROWSE-DEF-EXPR-5}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 5 &THEN
         when 6 then c-value = string(tt-browse.{&TT-Q-BROWSE-DEF-EXPR-6}).
         &ENDIF
         &IF {&Q-NUM-FILT-VALS} > 6 &THEN
         when 7 then c-value = string(tt-browse.{&TT-Q-BROWSE-DEF-EXPR-7}).
         &ENDIF
      END CASE.

   end.

   do with frame {&FRAME-NAME}:

      if valid-handle(global-drill-handle) then do:

         if global-drill-handle:private-data = "UPDATE" and
            c-browse-name <> "mg068" and
            available {&FIRST-TABLE-IN-QUERY-{&BROWSE-NAME}}
         then do:
            if global-drill-handle:data-type = "character" then
               global-drill-handle:screen-value =
                  string(c-value, global-drill-handle:format).
            else
               global-drill-handle:screen-value = string(c-value).
         end.

      end.

      &IF "{&WINDOW-SYSTEM}" = "TTY" &THEN
         if c-browse-name <> "mg068" then
            frame-value = string(c-value) NO-ERROR.
      &ENDIF

   end.

   if available (tt-browse) then
      swindow_recid = tt-browse.tt-recid.

   return "".

END PROCEDURE.

PROCEDURE re-open-tt-query :
/*------------------------------------------------------------------------------
  Purpose:     Opens the browse/query in the display
  Parameters:  <none>
  Notes:       This is not for the database query.
------------------------------------------------------------------------------*/

   do with frame {&FRAME-NAME}:
      c-scroll-msg:screen-value = "".
   end.

   open query BROWSE-2 for each tt-browse no-lock by tt-seq.

   run find-recid-and-screen-value.

END PROCEDURE.

PROCEDURE reposition-db-query :
/*------------------------------------------------------------------------------
  Purpose:     Position the query to read in the next chunk of data.
  Parameters:  p-direction: place to reposition to.
  Notes:
------------------------------------------------------------------------------*/
   define input parameter p-direction as character no-undo.

   CASE p-direction:
      when "FIRST" then run reposition-to-first in h-query.
      when "LAST"  then run reposition-to-last in h-query.
   END CASE.

END PROCEDURE.

PROCEDURE reposition-tt :
/*------------------------------------------------------------------------------
  Purpose:     To view the re-populated temp table, positioned to the right place
  Parameters:  p-{&ROW-ID}: {&ROW-ID} of the temp table record we were on before new data
                        was added to temp table.
               p-direction: character flag that indicates which direction to move
                            browse focus
  Notes:       We turn "refreshable" off and on to minimize flashing.
------------------------------------------------------------------------------*/
   define input parameter p-rowid as rowid no-undo.
   define input parameter p-direction as character no-undo.

   do with frame {&FRAME-NAME}:

      run re-open-tt-query.

      /*Prevent progress error if browse is blank*/
      if p-rowid <> ? then do:

         reposition {&BROWSE-NAME} to rowid p-rowid.

         if {&BROWSE-NAME}:select-focused-row() then .

         CASE p-direction:
            when "PREV" then do:
               if {&BROWSE-NAME}:select-prev-row() then .
               if {&BROWSE-NAME}:select-row(1) then .
            end.
            when "NEXT" then do:
               if {&BROWSE-NAME}:select-next-row() then .
               if {&BROWSE-NAME}:select-row({&BROWSE-NAME}:num-iterations) then .
            end.
         END CASE.

         apply "VALUE-CHANGED" to browse {&BROWSE-NAME}.

      end.

   end.

END PROCEDURE.

PROCEDURE reposition-tt-query :
/*------------------------------------------------------------------------------
  Purpose:     To capture the rowid of the temp table query so we can be at
               the right place to add new records.
  Parameters:  p-direction
  Notes:
------------------------------------------------------------------------------*/
   define input parameter p-direction as character no-undo.

   define variable i as integer no-undo.
   define variable i-temp-incr as integer no-undo.

   do with frame {&FRAME-NAME}:

      CASE p-direction:

         when "FIRST" then do:

            get first {&BROWSE-NAME}.

            r-temp-id = rowid(tt-browse).

            /*Position the query to the next record, this will be*/
            /*the top for display the next record set. We want   */
            /*browse to be positioned in the exact same place    */
            /*before going off-home.                             */
            get next {&BROWSE-NAME}.

            r-tempb-id = rowid(tt-browse).

         end.

         when "LAST"  then do:

            get last {&BROWSE-NAME}.
            r-temp-id = rowid(tt-browse).

            if i-temp-seq > 0 then do:
               i-temp-incr = tt-browse.tt-seq - i-temp-seq.
               if i-temp-incr > {&BROWSE-NAME}:num-iterations then
                  i-temp-incr = 0.
            end.

            /*Position the query to the last record, then go back*/
            /*for the number of fields in view. We want the      */
            /*browse to be positioned in the exact same place    */
            /*before going off-end.                              */
            do i = 1 to (({&BROWSE-NAME}:num-iterations - 1)):
               get prev {&BROWSE-NAME}.
            end.

            r-tempb-id = rowid(tt-browse).
         end.

      END CASE.

   end.

END PROCEDURE.

PROCEDURE set-query-handle :
/*------------------------------------------------------------------------------
  Purpose:     attaches the handle of the persistent procedure that contains the
               db query that will provide data to the temp table
  Parameters:  p-query-hdl
  Notes:       When the query changes, close the existing UI query (if there is
               one) and then get and display data from the new one.
------------------------------------------------------------------------------*/
   define input parameter p-query-hdl as handle no-undo.

   run close-query.  /* close the currently open browse */

   h-query = p-query-hdl.              /* assign new query */

   run get-first-chunk.    /* open and populate browse */

END PROCEDURE.

PROCEDURE getBrowseColumnLabel:
/*------------------------------------------------------------------------------
  Purpose:     Return the externalized column label for a browse column.
  Parameters:  input  i-col   Column number to return label for.
               output p-label Column label.
------------------------------------------------------------------------------*/
   define input parameter i-col as integer no-undo.
   define output parameter p-label as character no-undo.

   define variable h-widget as handle no-undo.
   define variable i as integer no-undo.

   /* SINCE LABELS ARE NOW DYNAMICALLY SET AT RUN TIME */
   /* LETS WALK THE BROWSE TO GET THE TRANSLATED COLUMN*/
   /* LABEL.                                           */
   h-widget = {&BROWSE-NAME}:handle in frame {&FRAME-NAME}.
   h-widget = h-widget:first-column.
   i = 1.

   if i-col <> 1 then do:
      do while valid-handle(h-widget):
         h-widget = h-widget:next-column.
         i = i + 1.
         if i = i-col then leave.
      end.
   end.

   if valid-handle(h-widget) then
      p-label = h-widget:label.

END PROCEDURE.
